<?php
namespace app\widgets;


use app\models\Book;
use app\models\Relationship;
use yii\base\Widget;

class RelationWidget extends Widget {

    /* @var $model Book */
    public $model;
    public $limit = 12;

    public function run() {
        $relationships = $this->model->relationships;
        $meta_id = array_keys($relationships);

        $query = Book::find();
        $table = Relationship::tableName();
        $query->leftJoin("{$table} b", 'b.owner_id = id');
        $query->andWhere(['b.meta_id' => $meta_id]);
        $query->andWhere(['<>','id', $this->model->id]);
        $query->orderBy('view_count desc');
        $query->groupBy('b.owner_id');
        $items = $query->asArray()->limit($this->limit)->all();
        return $this->render('/_data', compact('items'));
    }
}